/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.agil.photoalbum.web.servlets;

import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.*;
import javax.servlet.http.*;

import org.hibernate.Session;

import com.agil.photoalbum.db.dao.Functions;
import com.agil.photoalbum.db.entities.Category;
import com.agil.photoalbum.db.entities.Comment;
import com.agil.photoalbum.db.entities.Role;
import com.agil.photoalbum.db.entities.User;
import com.agil.photoalbum.db.util.SessionManager;

/**
 * 
 * @author Hovanessyan
 */
public class RegisterServlet extends HttpServlet {

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String userName = request.getParameter("persUserName");
		String pass = request.getParameter("persPass");
		String firstName = request.getParameter("persFirstName");
		String lastName = request.getParameter("persLastName");
		String email = request.getParameter("persEmail");
		Session session = Functions.getSession();

		User user = new User(1, userName, pass, firstName, lastName, email,
				new HashSet<Comment>(), new HashSet<Role>(),
				new HashSet<Category>());
		if (!Functions.userExists(userName)) {
			if (Functions.addUser(user)) {
				response.sendRedirect("pages/homePage.jsp");
			}
		} else {
//			Map parameters = request.getParameterMap();
//			setParameters(parameters, request);
			request.getSession().setAttribute("user",user);

			response.sendRedirect("pages/registerForm.jsp");
		}

	}

	private void setParameters(Map params, HttpServletRequest request) {

		Iterator i = params.keySet().iterator();
		while (i.hasNext()) {
			request.getSession().setAttribute((String) i.next(), params.get(i));
		}
	}

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
	// the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 */
	public String getServletInfo() {
		return "Short description";
	}
	// </editor-fold>
}
